package com.example.netty.udp.classloader;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ClassLoaderTests {

    public static void main(String[] args) throws ClassNotFoundException {
//        ClassLoaderTests.class.getClassLoader().loadClass(MyTest.class.getName());
        diskClassLoader();
    }

    public static void diskClassLoader() {
        //创建自定义classloader对象。
        DiskClassLoader diskLoader = new DiskClassLoader("E:\\gitee\\spring-agent\\jmeter\\target\\jmeter-0.0.1-SNAPSHOT.jar");
        try {
            //加载class文件
            Class c = diskLoader.loadClass("com.example.jmeter.entity.TestPlan");

            if(c != null){
                try {
                    Object obj = c.newInstance();
                    Method set = c.getDeclaredMethod("setName",String.class);
                    //通过反射调用Test类的say方法
                    set.invoke(obj, "null12345");
                    Method get = c.getDeclaredMethod("getName");
                    System.out.println(get.invoke(obj));
                } catch (InstantiationException | IllegalAccessException
                         | NoSuchMethodException
                         | SecurityException |
                         IllegalArgumentException |
                         InvocationTargetException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

